** Preparing CIA test

use "$project_path/data/4_regdata/regdata", clear
gen decade = Year - mod(Year, 10)
egen region_decade = group(Region decade)
tab region_decade, gen(d_region_decade)
local outcomes = "diff_m1_chix_econ std_diff_m1_hdi std_diff_m1_democracy_index diff_m1_general_chix"
local controls = "d_region_decade*"
keep if abs(runvar)<=10

keep Country Year Month Type_Election runvar treatment `controls' `outcomes'

** Appendix Figure E.5

foreach v in `outcomes' {

		* Computing p-values

		//Test on the left of the threshold
		qui reg `v' runvar `controls' if runvar<0
		local left_est : di %8.3f _b[runvar]
		local left_se : di %8.3f _se[runvar]
		local left_pval : di %8.3f 2*ttail(e(df_r),abs(_b[runvar]/_se[runvar])) // Two-sided test with ttail(degrees of freedom, t). Here, we compute: |t|>= abs(_b[runvar]/_se[runvar]) with e(df_r) degrees of freedom.
		local left_est = subinstr("`left_est'", " ", "", .)
		local left_se = subinstr("`left_se'", " ", "", .)
		local left_pval = subinstr("`left_pval'", " ", "", .)

		//Test on the right of the threshold
		qui reg `v' runvar `controls' if runvar>0 
		local right_est : di %8.3f _b[runvar]
		local right_se : di %8.3f _se[runvar]
		local right_pval : di %8.3f 2*ttail(e(df_r),abs(_b[runvar]/_se[runvar]))
		local right_est = subinstr("`right_est'", " ", "", .)
		local right_se = subinstr("`right_se'", " ", "", .)
		local right_pval = subinstr("`right_pval'", " ", "", .)

		//Residuals of the outcome variable, on both sides of the theshold
		qui reg `v' `controls' if treatment==0
		predict res_`v'0, residuals 
		qui reg `v' `controls' if treatment==1
		predict res_`v'1, residuals
		gen res_`v' = treatment*res_`v'1 + (1-treatment)*res_`v'0 

		local note = `" "CIA test (left of the cutoff):  `left_est' (`left_se'), p-val. = `left_pval'" "CIA test (right of the cutoff): `right_est' (`right_se'), p-val. = `right_pval'""'

		* Plot 

		if "`v'"=="diff_m1_chix_econ" local title = "Economic performance"
		if "`v'"=="std_diff_m1_hdi" local title = "Human Development Index"
		if "`v'"=="std_diff_m1_democracy_index" local title = "Democracy"
		if "`v'"=="diff_m1_general_chix" local title = "Country performance"

		//Preparing the binscatter
		// Creates a new categorical variable coded with the left-hand ends of the grouping intervals. From -`subframe' to `subframe_m1' with steps of size 2.
		egen temp_x = cut(runvar), at(-11(2)11) 
		replace temp_x = temp_x  + 1
		// In each bin, we compute the mean of the residuals of the outcome variable
		sort temp_x Country Year Month Type_Election
		by temp_x: egen temp_y = mean(res_`v') 
		by temp_x: gen temp_i = _n 

		// Twoway linear prediction plots with CIs
		twoway ///
		(lfitci res_`prefix'`v' runvar if runvar<0 & abs(runvar)<=10,  level(90) graphregion(color(white))) ///
		(lfitci res_`prefix'`v' runvar if runvar>0 & abs(runvar)<=10, level(90) graphregion(color(white))) ///
		(scatter temp_y temp_x if temp_i == 1 & abs(runvar)<=10,  /*ylabel(-0.3(0.1)0.3)*/ graphregion(color(white))), ///
		xtitle("Margin of victory of the challenger", size(large)) ytitle("Residuals", size(large)) xline(0) legend(off) xlabel(-10(5)10, labsize(large)) ylabel(-0.2(0.2)0.2, angle(0) labsize(large)) xticks(-10(5)10) note(`note', tstyle(size(medlarge))) title("`title'", size(large))
		drop temp_x temp_y temp_i

		if "`v'"=="diff_m1_chix_econ" local figure_name = "Appendix_Figure_E5a"
		if "`v'"=="std_diff_m1_hdi" local figure_name = "Appendix_Figure_E5b"
		if "`v'"=="std_diff_m1_democracy_index" local figure_name = "Appendix_Figure_E5c"
		if "`v'"=="diff_m1_general_chix" local figure_name = "Appendix_Figure_E5d"

		graph export "$project_path/output/appendix_figures/`figure_name'.pdf", as(pdf) name("Graph") replace

}
